home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_exmh.idb / usr / freeware / lib / exmh-2.5 / html.tcl.z / html.tcl
Text File  |  2002-07-08  |  9KB  |  316 lines

  1. # html.tcl
  2. # Use the HTML library from WebTk to display html documenation
  3. set WebTk(version) "Exmh HTML Browser 1.0"
  4.  
  5. proc Html_Init {} {
  6.     global window
  7.     Preferences_Add "Html Viewer" \
  8. "Exmh has a simple HTML viewer for its on-line documentation." {
  9.  
  10.     {window(fontsize) htmlFontAdjust {CHOICE 0 4 8} {Font size adjustment}
  11. "This setting adds to the font size used for HTML display."}
  12.     {Http(server) httpProxy {} {HTTP Proxy Server}
  13. "This sets the proxy used to make HTTP requests through a firewall.
  14. Leave this blank if you have no proxy."}
  15.     {Http(port) httpProxyPort 8080 {HTTP Proxy Port}
  16. "This is the port number for the proxy server."}
  17.     {WebTk(cache) htmlCacheDir /tmp/.webtkcache {Image Cache Directory}
  18. "This directory holds image data that is used for long term
  19. caching, such as between runs of the browser."}
  20.     {cachesize(max) htmlCacheSize 1000000 {Max Bytes in Cache Directory}
  21. "This limits the data stored in the image cache directory."}
  22.     {imagecachesize imageCacheSize 10 {Max Images Cached in Memory}
  23. "This limits the number of images saved in main memory.
  24. A setting of 0 minimizes memory use."}
  25.     {window(imagesEnabled) imageEnable ON {Enable display of in-line images}
  26. "Use this to enable or disable images in your HTML messages."}
  27. }
  28.     HtmlInitVars
  29. }
  30. proc HtmlInitVars {} {
  31.     global window
  32.     set window(colorAnchor) 0
  33.     if {![info exists window(fontsize)] ||
  34.     [string length $window(fontsize)] == 0} {
  35.     set window(fontsize) 0            ;# Magnifiy factor
  36.     }
  37.     if {![info exists window(indentsize)] ||
  38.     [string length $window(indentsize)] == 0} {
  39.     set window(indentsize) 0.6        ;# Indent width
  40.     }
  41.     Map_Init
  42.     trace variable window(fontsize) w HtmlFontSizeUpdate
  43.     trace variable cachesize(max) w CachePrefTrace
  44.     auto_load Content_text/html
  45.     # For installer
  46.     global WebTk
  47.     if ![info exists WebTk(cache)] {
  48.     set WebTk(cache) /tmp/.webtkcache
  49.     }
  50.     Cache_Init
  51. }
  52. proc HtmlFontSizeUpdate {args} {
  53.     global window html exwin
  54.     if {![info exists window(fontsize)] ||
  55.     [string length $window(fontsize)] == 0} {
  56.     set window(fontsize) 0            ;# Magnifiy factor
  57.     }
  58.     if [info exists html(win)] {
  59.     HMset_state $html(win) -size $window(fontsize)
  60.     }
  61.     HMset_state $exwin(mtext) -size $window(fontsize)
  62. }
  63. proc HtmlWindow {t} {
  64.     global window
  65.     if {[Exwin_Toplevel $t "Html Docs" Html] } {
  66.  
  67.     set f [frame $t.body]
  68.     set win [Widget_Text $f 30]
  69.     pack $f -side top -fill both -expand true
  70.     upvar #0 HM$win var
  71.  
  72.     set url $t.but
  73.     label $url.status -textvariable HM$win\(S_stat1) -width 6 \
  74.         -relief ridge -bd 2 -padx 9 -pady 3 -foreground blue
  75.     set var(S_feedback) $url.status
  76.  
  77.     entry $url.entry  -textvariable HM$win\(S_urlDisplay) -width 35
  78.     pack $url.status $url.entry -side left
  79.     pack $url.entry -expand true -fill x
  80.     bind $url.entry <Return> "HtmlOpen $win ; break"
  81.  
  82.     foreach b [Widget_GetButDef $url] {
  83.         Widget_AddButDef $url $b
  84.     }
  85.     foreach M [Widget_GetMenuBDef $url] {
  86.         set menu [Widget_AddMenuBDef $url $M {right padx 1 filly}]
  87.         ButtonMenuInner $menu
  88.     }
  89.     global window
  90.     if ![info exists window(colorAnchor)] {
  91.         HtmlInitVars
  92.     }
  93.     set var(S_stat2) ""                ;# message line
  94.     set var(S_url) file:[pwd]
  95.     HMinit_win $win
  96.     HMset_state $win -insert insert        ;# We use the "insert" mark
  97.     HMset_indent $win $window(indentsize)
  98.     HMset_state $win -size $window(fontsize)
  99.  
  100.     } else {
  101.     set win $t.body.t
  102.     wm deiconify $t
  103.     raise $t
  104.     }
  105.     focus $win
  106.     Frame_Reset $win
  107.     return $win
  108. }
  109. proc Window_Frame {win0 parent scrolling padx pady} {
  110.     global window
  111.     # make the interface
  112.  
  113.     set win [text $parent.text -padx $padx -pady $pady -takefocus 1 \
  114.     -width 0 -height 0]    ;# Let grid allocate all the space
  115.     if {[string compare $scrolling none] != 0} {
  116.     scrollbar $parent.scrollbar  -command "$parent.text yview"  -orient v
  117.     $win config -yscrollcommand "$parent.scrollbar set"
  118.     pack $parent.scrollbar -in $parent -side right -expand 0 -fill y
  119.     }
  120.     pack $win -in $parent -side left -fill both -expand 1 -padx 0 -pady 0
  121.  
  122.     upvar #0 HM$win var HM$win0 var0
  123.  
  124.     HMinit_win $win                ;# Reset display engine
  125.     HMreset_win $win
  126.     Head_SetColors $win0 $win
  127.     HMset_state $win -insert insert        ;# We use the "insert" mark
  128.     HMset_state $win -size $window(fontsize)
  129.     HMset_indent $win $window(indentsize)
  130.     Edit_Reset $win
  131.     Frame_Reset $win
  132.     Input_Reset $win
  133.  
  134.     set var(S_url) $var0(S_url)        ;# So relative src works.
  135.     # Keep a pointer to the main text widget for the page.
  136.     Window_SetMaster $win $win0
  137.  
  138.     return $win
  139. }
  140. proc Html_Window {href} {
  141.     global html
  142.     set html(win) [HtmlWindow .html]
  143.     Html_HistoryAdd $html(win) $href
  144.     HMlink_callback $html(win) $href
  145.     return $html(win)
  146. }
  147. proc Html_Display {markup base} {
  148.     global html
  149.     set html(win) [HtmlWindow .html]
  150.     upvar #0 HM$html(win) var
  151.     set var(S_url) $base
  152.     # Do this *after* the environment is complete.
  153.     after 1 [list Url_DisplayHtml $html(win) $base $markup]
  154.     return $html(win)
  155. }
  156. proc Html_MimeShow {win part} {
  157.     upvar #0 HM$win var
  158.     global mimeHdr window
  159.  
  160.     if {![info exists var]} {
  161.     Map_Init
  162.     HMinit_win $win
  163.     HMset_state $win -insert insert        ;# We use the "insert" mark
  164.     HMset_indent $win $window(indentsize)
  165.     HMset_state $win -size $window(fontsize)
  166.     # Change URL hits to display in new window
  167.     $win tag bind link <Button-1> [list HtmlHit $win %x %y]
  168.     $win tag bind link <Shift-Button-1> ""
  169.     $win tag bind link <Double-Button-1> ""
  170.  
  171.     }
  172.     set var(S_exmhpart) $part
  173.     if [info exists mimeHdr($part,hdr,content-base)] {
  174.     set var(S_url) $mimeHdr($part,hdr,content-base)
  175.     } else {
  176.     set var(S_url) file:$mimeHdr($part,file)
  177.     }
  178.     if [catch {open $mimeHdr($part,file)} in] {
  179.     $win insert insert "Cannot open temp file: $in\n"
  180.     } else {
  181.     set html [read $in]
  182.     close $in
  183.     # Avoid showing frames in the main message area
  184.     if [regexp -nocase <frameset $html] {
  185.         $win insert insert "Showing frames in external viewer\n"
  186.         URI_StartViewer $var(S_url)
  187.     } else {
  188.         $win config -wrap word
  189.         Html_DisplayInline $win $var(S_url) $html
  190.     }
  191.     }
  192. }
  193. proc Html_Stop {win} {
  194.     upvar #0 HM$win var
  195.     if [info exists var] {
  196.     HMset_state $win -stop 1
  197.     }
  198.     Head_ResetColors $win    ;# reset window
  199.     Head_Reset $win        ;# clear memory
  200.     $win configure -tabs {}
  201.     $win config -wrap [option get $win wrap Text]
  202. }
  203. proc HtmlHit {win x y} {
  204.     upvar #0 HM$win var
  205.     lassign {href name} [UrlGetLink $win $x $y]
  206.     UrlResolve $var(S_url) href
  207.     URI_StartViewer $href
  208. }
  209. proc Html_DisplayInline {win url html} {
  210.     upvar #0 HM$win var
  211.     global window
  212.  
  213.     $win config -cursor watch -state normal
  214.     set var(S_urlDisplay) $url
  215.     catch {unset var(S_urlPending)}
  216.     HMreset_win $win 0            ;# Set display state
  217.     HMset_indent $win $window(indentsize) ;# Restore tab stops
  218.     HMset_state $win -insert insert    ;# We use the "insert" mark
  219.     Embed_Reset $win            ;# Nuke applets
  220.     Feedback $win busy
  221.     HMset_state $win -update 10        ;# Frequent updates during 1st display
  222.     HMparse_html $html [list HMrender $win]
  223.     Feedback $win ready
  224.     $win config -cursor [option get $win cursor Text]
  225. }
  226.  
  227. proc HtmlOpen {win} {
  228.     upvar #0 HM$win var
  229.     Html_HistoryAdd $win $var(S_urlDisplay)
  230.     HMlink_callback $win $var(S_urlDisplay)
  231. }
  232. proc Html_HistoryAdd {win url} {
  233.     upvar #0 HM$win var
  234.     UrlResolve $var(S_url) url
  235.     lappend var(S_history) $url
  236.     set var(S_origin) [expr [llength $var(S_history)] -1]
  237. }
  238. proc Html_Back {} {
  239.     global html
  240.     upvar #0 HM$html(win) var
  241.     if {$var(S_origin) > 0} {
  242.     incr var(S_origin) -1
  243.     set url [lindex $var(S_history) $var(S_origin)]
  244.     HMlink_callback $html(win) $url
  245.     }
  246. }
  247. proc Html_Forward {} {
  248.     global html
  249.      upvar #0 HM$html(win) var
  250.     if ![info exists var(S_origin)] {
  251.     return
  252.     }
  253.     if {$var(S_origin) < [llength $var(S_history)]-1} {
  254.     incr var(S_origin)
  255.     set url [lindex $var(S_history) $var(S_origin)]
  256.     HMlink_callback $html(win) $url
  257.     }
  258. }
  259. # win is an embedded window inside masterwin.  win displays a frame or table.
  260. proc Window_SetMaster {win masterwin} {
  261.     upvar #0 HM$masterwin var
  262.     upvar #0 HM$win var2
  263.     if [info exists var(S_mainwin)] { 
  264.     set var2(S_mainwin) $var(S_mainwin)
  265.     } else {
  266.     set var2(S_mainwin) $masterwin
  267.     }
  268. }
  269. proc Window_GetMaster {win} {
  270.     upvar #0 HM$win var
  271.     if [info exists var(S_mainwin)] {
  272.     # Level of indirection to support nested text widgets for tables
  273.     return $var(S_mainwin)
  274.     } else {
  275.     return $win
  276.     }
  277. }
  278.  
  279. proc Status {win string} {
  280.     Exmh_Status $string
  281. }
  282. proc Status_push {win string} {
  283.     Exmh_Status $string
  284. }
  285. proc Status_pop {win} {
  286.     Exmh_Status " "
  287. }
  288. proc Feedback { win word } {
  289.     upvar #0 HM$win var
  290.     set var(S_stat1) $word
  291.     catch {after cancel $var(S_after)}
  292.     catch {
  293.     set bg [lindex [$var(S_feedback) config -background] 3]
  294.     $var(S_feedback) config -bg $bg  -fg blue
  295.     }
  296.     update idletasks
  297. }
  298.  
  299. proc FeedbackLoop { win word } {
  300.     upvar #0 HM$win var
  301.     set var(S_stat1) $word
  302.     catch {after cancel $var(S_after)}
  303.     catch {
  304.     set bg [$var(S_feedback) cget -bg]
  305.     set def [lindex [$var(S_feedback) config -background] 3]
  306.     if {[string compare $bg $def] == 0} {
  307.         $var(S_feedback) config -bg white
  308.     } else {
  309.         $var(S_feedback) config -bg $def
  310.     }
  311.     set var(S_after) [after 200 [list FeedbackLoop $win $word]]
  312.     }
  313.     update idletasks
  314. }
  315.  
  316.